const pool = require("@/db");
const table = require("@/db/table");

exports.getNbThingOptions = async () => {
  return await pool.select("*").from(table.NBTHINGOPTION).queryList();
};

exports.getNbThingOption = async ({ id, title }) => {
  return await pool
    .select("*")
    .from(table.NBTHINGOPTION)
    .where("id", id, "eq", "ifHave")
    .where("title", title, "eq", "ifHave")
    .queryRow();
};

exports.createNbThingOption = async (row) => {
  const result = await pool
    .insert(table.NBTHINGOPTION, {
      ...row,
      created_at: new Date(),
      updated_at: new Date(),
    })
    .execute();
  return result.insertId;
};

exports.updateNbThingOption = async (id, row) => {
  const result = await pool
    .update(table.NBTHINGOPTION, row)
    .where("id", id)
    .execute();
  return result.affectedRows > 0;
};
